import {createRouter, createWebHistory} from 'vue-router'
import store from "../store";
import {showFailToast} from "vant";

const HomeView = () => import('../views/home/HomeView.vue');
const CategoryView = () => import('../views/category/CategoryView.vue');
const DetailView = () => import('../views/detail/DetailView.vue');
const ShopCartView = () => import('../views/shopcart/ShopCartView.vue');
const ProfileView = () => import('../views/profile/ProfileView.vue');
const RegisterView = () => import('../views/profile/RegisterView.vue');
const LoginView = () => import('../views/profile/LoginView.vue');
const AddressView = () => import('../views/profile/AddressView.vue')
const AddressEditView = () => import('../views/profile/AddressEditView.vue')
const CreateOrderView = () => import('../views/order/CreateOrderView.vue')
const OrderDetailView = () => import('../views/order/OrderDetailView.vue')
const OrderListView = () => import('../views/order/OrderListView.vue')
const AboutView = () => import('../views/Mine/AboutView.vue')
const AccountManagementView = () => import('../views/Mine/AccountManagementView.vue')
const routes = [
	{
		path: '/',
		name: 'home',
		component: HomeView,
		meta: {
			title: '图书兄弟'
		}
	},
	{
		name: 'category',
		path: '/category',
		component: CategoryView,
		meta: {
			title: '图书兄弟-商品分类'
		}
	},
	{
		name: 'detail',
		path: '/detail',
		component: DetailView,
		meta: {
			title: '图书兄弟-商品详情'
		}
	},
	{
		name: 'shopcart',
		path: '/shopcart',
		component: ShopCartView,
		meta: {
			title: '图书兄弟-购物车',
			isAutoRequired: true
		}
	},
	{
		name: 'profile',
		path: '/profile',
		component: ProfileView,
		meta: {
			title: '图书兄弟-个人中心',
			isAutoRequired: true
		}
	},
	{
		name: 'address',
		path: '/address',
		component: AddressView,
		meta: {
			title: '图书兄弟-地址管理',
			isAutoRequired: true
		}
	},
	{
		name: 'addressedtt',
		path: '/addressedit',
		component: AddressEditView,
		meta: {
			title: '图书兄弟-编辑地址',
			isAutoRequired: true
		}
	},
	{
		name: 'register',
		path: '/register',
		component: RegisterView,
		meta: {
			title: '图书兄弟-用户注册'
		}
	},
	{
		name: 'login',
		path: '/login',
		component: LoginView,
		meta: {
			title: '图书兄弟-用户登录'
		}
	},
	{
		name: 'orderlist',
		path: '/orderlist',
		component: OrderListView,
		meta: {
			title: '图书兄弟-订单列表',
			isAutoRequired: true
		}
	},
	{
		name: 'createorder',
		path: '/createorder',
		component: CreateOrderView,
		meta: {
			title: '图书兄弟-创建订单',
			isAutoRequired: true
		}
	},
	{
		name: 'orderdetail',
		path: '/orderdetail',
		component: OrderDetailView,
		meta: {
			title: '图书兄弟-订单详情',
			isAutoRequired: true
		}
	},
	{
		name: 'accountmanagement',
		path: '/accountmanagement',
		component: AccountManagementView,
		meta: {
			title: '图书兄弟-订单详情',
			isAutoRequired: true
		}
	},
	{
		name: 'about',
		path: '/about',
		component: AboutView,
		meta: {
			title: '图书兄弟-订单详情',
			isAutoRequired: true
		}
	},
	{
		name: '404',
		path: '/404',
		component: HomeView
	}
]

const router = createRouter({
	history: createWebHistory(process.env.BASE_URL),
	routes
})
router.beforeEach((to, from, next) => {
	// 判断要到达的页面是否需要token授权
	if (to.meta['isAutoRequired'] && store.state.user.isLogin === false) {
		showFailToast('请先登录');
		return next('/login')
	} else {
		next()
	}

	const {title} = to.meta;
	document.title = title
})
export default router
